
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>本地格式化 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="Time zones" href="timezones.html" />
    <link rel="prev" title="翻译" href="translation.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="translation.html" title="翻译">previous</a>
     |
    <a href="../index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="timezones.html" title="Time zones">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-i18n-formatting">
            
  <div class="section" id="s-format-localization">
<span id="format-localization"></span><h1>本地格式化<a class="headerlink" href="#format-localization" title="永久链接至标题">¶</a></h1>
<div class="section" id="s-overview">
<span id="overview"></span><h2>概况<a class="headerlink" href="#overview" title="永久链接至标题">¶</a></h2>
<p>Django 的格式化系统可以在模板中使用指定的格式化工具为当前的 <a class="reference internal" href="index.html#term-locale-name"><span class="xref std std-term">locale</span></a> 来显示日期、时间和数字。</p>
<p>启用后，访问相同内容的两个用户可能看见不同的日期、时间和数字，这取决于他们本地的语言环境。</p>
<p>格式化系统默认是关闭的。如果要启用它，在配置文件里设置 <a class="reference internal" href="../../ref/settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span> <span class="pre">=</span> <span class="pre">True</span></code></a> 即可。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">方便起见，<code class="xref std std-djadmin docutils literal notranslate"><span class="pre">django-admin</span> <span class="pre">startproject</span> <span class="pre">1</span></code> 创建 <code class="file docutils literal notranslate"><span class="pre">settings.py</span></code> 文件，包含 <a class="reference internal" href="../../ref/settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span> <span class="pre">=</span> <span class="pre">True</span></code></a> 的 。注意，启用千分位的数字格式化需要在配置文件里设置 <a class="reference internal" href="../../ref/settings.html#std:setting-USE_THOUSAND_SEPARATOR"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_THOUSAND_SEPARATOR</span> <span class="pre">=</span> <span class="pre">True</span></code></a> 。或者，在模板里可以使用  <a class="reference internal" href="../../ref/contrib/humanize.html#std:templatefilter-intcomma"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">intcomma</span></code></a> 来格式化数字。</p>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">还有一个有关 <a class="reference internal" href="../../ref/settings.html#std:setting-USE_I18N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_I18N</span></code></a> 的设置，用来控制 Django 是否可以动态翻译。查看 <a class="reference internal" href="translation.html"><span class="doc">翻译</span></a> 获取更多细节。</p>
</div>
</div>
<div class="section" id="s-locale-aware-input-in-forms">
<span id="locale-aware-input-in-forms"></span><h2>表单中感知本地语言环境输入<a class="headerlink" href="#locale-aware-input-in-forms" title="永久链接至标题">¶</a></h2>
<p>格式化开启后，当在表单里解析日期、时间和数字时，Django 可以使用本地化格式。这意味着猜测用户在表单上的输入时所使用的格式化时，会对不同语言环境尝试不同的格式化。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">Django 使用与解析数据不同的格式来显示数据。最值得注意的是，解析日期的格式化不能使用 <code class="docutils literal notranslate"><span class="pre">%a</span></code> （缩写的日期），<code class="docutils literal notranslate"><span class="pre">%A</span></code> （完整的日期），<code class="docutils literal notranslate"><span class="pre">%b</span></code> （缩写的月份），<code class="docutils literal notranslate"><span class="pre">%B</span></code> （完整的月份），或者 <code class="docutils literal notranslate"><span class="pre">%p</span></code> (AM/PM)。</p>
</div>
<p>使用 <code class="docutils literal notranslate"><span class="pre">localize</span></code> 参数来使表单字段能本地化输入和输出数据：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">CashRegisterForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
   <span class="n">product</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
   <span class="n">revenue</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DecimalField</span><span class="p">(</span><span class="n">max_digits</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">decimal_places</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">localize</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-controlling-localization-in-templates">
<span id="s-topic-l10n-templates"></span><span id="controlling-localization-in-templates"></span><span id="topic-l10n-templates"></span><h2>在模板中控制本地化<a class="headerlink" href="#controlling-localization-in-templates" title="永久链接至标题">¶</a></h2>
<p>使用 <a class="reference internal" href="../../ref/settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> 启用格式化后，每当 Django 在模板中输入数值时，它会试着使用本地语言环境指定的格式化。</p>
<p>然而，使用本地化值可能并不总是合适的——比如，如果你正在输出机器可读的 JavaScript 或 XML ，则始终需要非本地化的值。你也可以在已选的模板里使用本地化，而不是在所有地方使用。</p>
<p>为了更好的控制本地化的使用，Django 提供 <code class="docutils literal notranslate"><span class="pre">l10n</span></code> 模板库，它包含以下的  tags 和 filters。</p>
<div class="section" id="s-template-tags">
<span id="template-tags"></span><h3>模板标签<a class="headerlink" href="#template-tags" title="永久链接至标题">¶</a></h3>
<div class="section" id="s-localize">
<span id="s-std:templatetag-localize"></span><span id="localize"></span><span id="std:templatetag-localize"></span><h4><code class="docutils literal notranslate"><span class="pre">localize</span></code><a class="headerlink" href="#localize" title="永久链接至标题">¶</a></h4>
<p>在模板里启用和禁用模板变量本地化。</p>
<p>相比 <a class="reference internal" href="../../ref/settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> ，这个标签可对本地化进行更精细的控制。</p>
<p>要为模板块激活或禁用本地化，请用：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="o">%</span> <span class="n">load</span> <span class="n">l10n</span> <span class="o">%</span><span class="p">}</span>

<span class="p">{</span><span class="o">%</span> <span class="n">localize</span> <span class="n">on</span> <span class="o">%</span><span class="p">}</span>
    <span class="p">{{</span> <span class="n">value</span> <span class="p">}}</span>
<span class="p">{</span><span class="o">%</span> <span class="n">endlocalize</span> <span class="o">%</span><span class="p">}</span>

<span class="p">{</span><span class="o">%</span> <span class="n">localize</span> <span class="n">off</span> <span class="o">%</span><span class="p">}</span>
    <span class="p">{{</span> <span class="n">value</span> <span class="p">}}</span>
<span class="p">{</span><span class="o">%</span> <span class="n">endlocalize</span> <span class="o">%</span><span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">在 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">localize</span> <span class="pre">%}</span></code> 块中，<a class="reference internal" href="../../ref/settings.html#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> 的值会被忽略。</p>
</div>
<p>查看 <a class="reference internal" href="#std:templatefilter-localize"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">localize</span></code></a> 和 <a class="reference internal" href="#std:templatefilter-unlocalize"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">unlocalize</span></code></a> 来获取在每个变量基础上执行相同任务的模板过滤器。</p>
</div>
</div>
<div class="section" id="s-template-filters">
<span id="template-filters"></span><h3>模板过滤器<a class="headerlink" href="#template-filters" title="永久链接至标题">¶</a></h3>
<div class="section" id="s-std:templatefilter-localize">
<span id="s-id1"></span><span id="std:templatefilter-localize"></span><span id="id1"></span><h4><code class="docutils literal notranslate"><span class="pre">localize</span></code><a class="headerlink" href="#std:templatefilter-localize" title="永久链接至标题">¶</a></h4>
<p>对单一值的强制本地化</p>
<p>例如:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="o">%</span> <span class="n">load</span> <span class="n">l10n</span> <span class="o">%</span><span class="p">}</span>

<span class="p">{{</span> <span class="n">value</span><span class="o">|</span><span class="n">localize</span> <span class="p">}}</span>
</pre></div>
</div>
<p>对单一值取消本地化，使用 <a class="reference internal" href="#std:templatefilter-unlocalize"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">unlocalize</span></code></a> 。要控制大部分模板的本地化，请使用 <a class="reference internal" href="#std:templatetag-localize"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">localize</span></code></a> 模板标签。</p>
</div>
<div class="section" id="s-unlocalize">
<span id="s-std:templatefilter-unlocalize"></span><span id="unlocalize"></span><span id="std:templatefilter-unlocalize"></span><h4><code class="docutils literal notranslate"><span class="pre">unlocalize</span></code><a class="headerlink" href="#unlocalize" title="永久链接至标题">¶</a></h4>
<p>强制地让单一值不被本地化。</p>
<p>例如:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="o">%</span> <span class="n">load</span> <span class="n">l10n</span> <span class="o">%</span><span class="p">}</span>

<span class="p">{{</span> <span class="n">value</span><span class="o">|</span><span class="n">unlocalize</span> <span class="p">}}</span>
</pre></div>
</div>
<p>对单一值强制本地化，使用 <a class="reference internal" href="#std:templatefilter-localize"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">localize</span></code></a> 。要控制大部分模板的本地化，请使用 <a class="reference internal" href="#std:templatetag-localize"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">localize</span></code></a> 模板标签。</p>
<p>返回未本地化数字（<code class="docutils literal notranslate"><span class="pre">int</span></code>、<code class="docutils literal notranslate"><span class="pre">float</span></code> 或 <code class="docutils literal notranslate"><span class="pre">Decimal</span></code>）的字符串表示。</p>
</div>
</div>
</div>
<div class="section" id="s-creating-custom-format-files">
<span id="s-custom-format-files"></span><span id="creating-custom-format-files"></span><span id="custom-format-files"></span><h2>创建自定义的格式化文件<a class="headerlink" href="#creating-custom-format-files" title="永久链接至标题">¶</a></h2>
<p>Django provides format definitions for many locales, but sometimes you might
want to create your own, because a format file doesn't exist for your locale,
or because you want to overwrite some of the values.</p>
<p>要使用自定义格式化，你首先要指定格式化文件的路径，需要将 <a class="reference internal" href="../../ref/settings.html#std:setting-FORMAT_MODULE_PATH"><code class="xref std std-setting docutils literal notranslate"><span class="pre">FORMAT_MODULE_PATH</span></code></a> 设置为格式化文件所在的包路径，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">FORMAT_MODULE_PATH</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;mysite.formats&#39;</span><span class="p">,</span>
    <span class="s1">&#39;some_app.formats&#39;</span><span class="p">,</span>
<span class="p">]</span>
</pre></div>
</div>
<p>文件不能直接放置在这个目录里，而是要放在本地语言环境的目录中，而且必须命名为 <code class="docutils literal notranslate"><span class="pre">formats.py</span></code> 。要注意不要在这些文件里放置敏感信息，因为如果你把字符串传递给 <code class="docutils literal notranslate"><span class="pre">django.utils.formats.get_format()</span></code> （使用 <a class="reference internal" href="../../ref/templates/builtins.html#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 模板过滤器），那么内部值会被暴露。</p>
<p>要想自定义英语格式化，需要这样的结构：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">mysite</span><span class="o">/</span>
    <span class="n">formats</span><span class="o">/</span>
        <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
        <span class="n">en</span><span class="o">/</span>
            <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
            <span class="n">formats</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p><code class="file docutils literal notranslate"><span class="pre">formats.py</span></code> 包含自定义格式化定义。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">THOUSAND_SEPARATOR</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\xa0</span><span class="s1">&#39;</span>
</pre></div>
</div>
<p>使用不换行空格（硬空格 Unicode <code class="docutils literal notranslate"><span class="pre">00A0</span></code> ）作为千位分隔符，而不是英语的默认逗号。</p>
</div>
<div class="section" id="s-limitations-of-the-provided-locale-formats">
<span id="limitations-of-the-provided-locale-formats"></span><h2>提供本地语言环境格式化的局限性<a class="headerlink" href="#limitations-of-the-provided-locale-formats" title="永久链接至标题">¶</a></h2>
<p>一些本地语言环境为数字使用上下文相关的格式化，而 Django 本地化系统不能自动处理。</p>
<div class="section" id="s-switzerland-german">
<span id="switzerland-german"></span><h3>Switzerland (German)<a class="headerlink" href="#switzerland-german" title="永久链接至标题">¶</a></h3>
<p>瑞士数字格式化取决于要格式化的数字类型。对于货币值，用逗号作为千位分隔符，小数点作为小数点分隔符。对于其他数字，逗号用作小数点分隔符，空格用作千分位分隔符。Django 提供的本地语言环境格式化使用通用分隔符，小数点用逗号，千分位分隔符用空格。</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">本地格式化</a><ul>
<li><a class="reference internal" href="#overview">概况</a></li>
<li><a class="reference internal" href="#locale-aware-input-in-forms">表单中感知本地语言环境输入</a></li>
<li><a class="reference internal" href="#controlling-localization-in-templates">在模板中控制本地化</a><ul>
<li><a class="reference internal" href="#template-tags">模板标签</a><ul>
<li><a class="reference internal" href="#localize"><code class="docutils literal notranslate"><span class="pre">localize</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#template-filters">模板过滤器</a><ul>
<li><a class="reference internal" href="#std:templatefilter-localize"><code class="docutils literal notranslate"><span class="pre">localize</span></code></a></li>
<li><a class="reference internal" href="#unlocalize"><code class="docutils literal notranslate"><span class="pre">unlocalize</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#creating-custom-format-files">创建自定义的格式化文件</a></li>
<li><a class="reference internal" href="#limitations-of-the-provided-locale-formats">提供本地语言环境格式化的局限性</a><ul>
<li><a class="reference internal" href="#switzerland-german">Switzerland (German)</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="translation.html"
                        title="上一章">翻译</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="timezones.html"
                        title="下一章">Time zones</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/i18n/formatting.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="translation.html" title="翻译">previous</a>
     |
    <a href="../index.html" title="使用 Django" accesskey="U">up</a>
   |
    <a href="timezones.html" title="Time zones">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>